home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Internet Info 1994 March
/
Internet Info CD-ROM (Walnut Creek) (March 1994).iso
/
networking
/
ip
/
ka9q
/
xobbs.arc
/
maildaemon.c
< prev
next >
Wrap
C/C++ Source or Header
|
1989-05-07
|
8KB
|
248 lines
/* MAILDAEMON.C Mail daemon process for XOBBS. Jim Durham, W2XO 4-4-89 */
/* Version 1.3 */
/* Code released to the amateur radio community */
#define MAIN 1
#define MAILDAEMON
#include "xobbs.h"
#include <string.h>
#ifndef hpux
#include <sys/signal.h>
#endif
char buf[4096];
void exit();
int domail();
main()
{
umask(0);
getconfig();
signal(SIGUSR1,domail);
for(;;){
pause();
signal(SIGUSR1,domail);
}
}
domail()
{
char filnam[40],c,*bp,*cp;
FILE *fp;
char temp[20];
int fd1,i;
signal(SIGUSR1,SIG_IGN);
gethighnum(1); /*update the highest message number*/
sprintf(prinbuf,"ls %s",tempdir);
fp=popen(prinbuf,"r"); /*get the temp directory*/
i=0;
while(((c=fgetc(fp)) != EOF) && (i < 4095)) /*read in the names*/
buf[i++]=c;
buf[i]='\0'; /*terminate the names*/
pclose(fp); /*close the pipe*/
bp=buf; /*point to the first name in the buffer*/
/*loop and service each file in the temp directory*/
for(;;){
if(*bp=='\n') bp++; /*if sitting on a lf, increment*/
if(*bp=='\0') break; /*if end of buffer, leave*/
cp=filnam; /*point to the filname space*/
while(*bp != '\n')
*cp++ = *bp++; /*read up to next lf into filnam*/
*cp='\0'; /*terminate the filname*/
checkfwd(filnam);
}/* for(;;) */ /*loop back for more names*/
/*now, open the parameter file and update it*/
if((fd1=open(paramfil,O_WRONLY|O_CREAT,0x1b6)) < 0){ /*open param file*/
sprintf(prinbuf,"maildaemon:Cant open %s\n",paramfil);
perror(prinbuf);
exit(1);
}
sprintf(temp,"%6d",highnum);
write(fd1,temp,(strlen(temp)+1));
close(fd1);
} /*and go home*/
checkfwd(filnam)
char *filnam; /* file name of file to be checked*/
{
int fd;
int fd1;
int fd2;
char hdr[128];
char fhdr[128];
char temp[128];
char *p;
char bbscall[8];
struct msghdr mhdr;
int done;
done = 0;
sprintf(temp,"%s%s",tempdir,filnam);
if((fd = open(temp,O_RDONLY)) < 1){ /*open the message file*/
sprintf(prinbuf,"checkfwd:Cant open message file\n");
perror(prinbuf);
close(fd);
return;
}
fgetline(fd,hdr); /*read the header info into buffer*/
conhdr(hdr,&mhdr); /*get header into header structure form*/
sprintf(fhdr,"%-5d %-85.85s\n",highnum,hdr); /*get ready for possible*/
/*write of header*/
/*first, get stuff for THIS bbs */
if( ( (!(strcmp(mhdr.tocall,my.call))) || (!(strcmp(mhdr.atbbs,my.call))))
|| ((mhdr.atbbs[0] == '\0') || (mhdr.stat == 'F'))){
/* if this is to my call */
/*or no atbbs call at all*/
/* then, we do only a move*/
/* to OUR mail directory*/
close(fd); /*close the file, we will use system call*/
/* write it to the mail directory*/
sprintf(temp,"mv %s%s %s%d",tempdir,filnam,maildir,highnum);
system(temp);
writnewhdr(fhdr); /*write out the new header */
highnum++; /*increment the high num*/
return;
}
close(fd);
if((fd1 = open(usenetfil,O_RDONLY)) < 1){ /*open distribution file*/
sprintf(prinbuf,"checkfwd: Can't open distribution file\n");
perror(prinbuf);
close(fd1);
return;
}
done = 0;
while(fgetline(fd1,inline)){ /*read a line from usenet file*/
if(!strcmp(mhdr.tocall,upcase(inline))){ /*if this guy is on usenet*/
sprintf(temp,"mail -s mhdr.title %s < %s%s",lowcase(inline),tempdir,filnam);
system(temp);
sprintf(temp,"%s%s",tempdir,filnam);
unlink(temp);
return;
}
}
close(fd1);
if((fd1 = open(fwddist,O_RDONLY)) < 1){ /*open distribution file*/
sprintf(prinbuf,"checkfwd: Can't open distribution file\n");
perror(prinbuf);
close(fd1);
return;
}
done = 0;
while(fgetline(fd1,inline)){ /*read a line from distribution file*/
if(inline[0] == '$'){
rdfield(&inline[1],temp); /*get the dist. net name*/
if(!strncmp(temp,mhdr.atbbs,strlen(mhdr.atbbs))){
/*if @bbs matches dist. name*/
/* then read up to next *** */
/*first, make a copy for THIS bbs */
sprintf(temp,"mv %s%s %s%d",tempdir,filnam,maildir,highnum);
system(temp);
writnewhdr(fhdr);
/*make distribution copies*/
while((fgetline(fd1,inline)) && (inline[0] != '*')){
sprintf(temp,"ln %s%d %s%s%s"
,maildir,highnum,fwddir,inline,filnam);
system(temp);
}
highnum++; /*increment the high num*/
done = 1;
break;
}
}
} /*while*/
close(fd1); /*close the distribution file*/
if(done) /* if we are done, return*/
return(1);
/* let's check the forwarding file */
if((fd=open(fwdfile,O_RDONLY)) < 1){ /*open the forwarding file*/
sprintf(prinbuf,"checkfwd: Can't open forwarding file\n");
perror(prinbuf);
return(0) ;
}
while(fgetline(fd,inline)){ /*get a line from the forwarding file*/
switch(inline[0]){
case 'G':
p=inline; /*point to the line*/
while(*p++ != ' '); /*toggle up to the space*/
rdfield(p,bbscall); /*get the bbs call from the line */
break;
case 'C': /*connect line*/
break;
case 'S': /* 'send' line*/
break;
case 'R': /* response line*/
break;
default:
if((matchn2(mhdr.atbbs,inline,strlen(mhdr.atbbs)))
|| (matchn2(mhdr.tocall,inline,strlen(mhdr.atbbs)))){
if(mhdr.type == 'B'){ /* if this is a bulletin*/
sprintf(temp,"cp %s%s %s%s%s"
,tempdir,filnam,fwddir,bbscall,filnam);
system(temp);
sprintf(temp,"mv %s%s %s%d",tempdir,filnam,maildir,highnum);
system(temp);
writnewhdr(fhdr);
highnum++; /*increment the high num*/
}
else{
sprintf(temp,"mv %s%s %s%s%s"
,tempdir,filnam,fwddir,bbscall,filnam);
system(temp);
}
} /*if !strcmp() */
break;
} /* switch*/
}
close(fd);
return(1);
}
writnewhdr(hdr)
char *hdr;
{
int fd,fd1,n;
char hdrtmp[30];
char oldhdr[30];
sprintf(hdrtmp,"%shdrtmp",homedir);
sprintf(oldhdr,"%soldhdr",homedir);
if((fd = open(hdrtmp,O_CREAT|O_RDWR,0666)) < 1){ /*open temp hdr file*/
perror("Maildaemon:writnewhdr():Cant make hdr temp file\n");
return;
}
write(fd,hdr,92); /*write new header*/
close(fd);
sprintf(prinbuf,"mv %s %s",hdrfile,oldhdr); /*save old header file*/
system(prinbuf);
sprintf(prinbuf,"cat %s %s > %s",hdrtmp,oldhdr,hdrfile);
system(prinbuf);
}